package pkg

import (
	"fmt"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
	"github.com/alibabacloud-go/tea/tea"
)

const (
	AccessKeyId     = "LTAI5tA9AjMxf3RqU9qzP4gB"
	AccessKeySecret = "fqzGDkKQUROeTAYq3uzqe04k49H9eF"
)

func SendCode(mobile string, code string) (*dysmsapi.SendSmsResponse, error) {
	con := &openapi.Config{
		// 您的AccessKey ID
		AccessKeyId: tea.String(AccessKeyId),
		// 您的AccessKey Secret
		AccessKeySecret: tea.String(AccessKeySecret),
	}
	// 访问的域名
	con.Endpoint = tea.String("dysmsapi.aliyuncs.com")
	client, err := dysmsapi.NewClient(con)
	if err != nil {
		return nil, err
	}
	request := &dysmsapi.SendSmsRequest{
		PhoneNumbers:  tea.String(mobile),
		SignName:      tea.String("阿里云短信测试"),
		TemplateCode:  tea.String("SMS_154950909"),
		TemplateParam: tea.String("{\"code\":\"" + code + "\"}"),
	}

	response, err := client.SendSms(request)
	fmt.Println(response)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	fmt.Println(tea.StringValue(response.Body.Code))
	return response, nil
}
